"Current" Module advise

I offer this general advise on the "current" module.

[1] When dealing with a module you should open it (or otherwise get it's open Module handle) and keep track of the variable with that handle so you can use them when needed. Thus, these two may be routine:

  • Module mHere = read(Name, false, true)
  • Module mThere = read(fullName(target(lnkRef)), false, true)


[2] Because the "current" module often changes (sometimes unpredictably) when modules are opened, one should rarely if ever do this:

 

  • Module m = current


[2a] The main exception is for scripts that are intended to run on the module used to invoke them (such as a CountObjects script); those script should routinely do this ONCE at the top of the script; so the script knows which module is the primary or home module for this script execution.

 

 

  • Module mHome = current
  • if (null mHome) then {errorBox("Run from open module"); halt}


Other scripts do NOT run from a home module (such as listing modules in a folder):

 

 

  • Module mHome = current
  • if (! null mHome) then {errorBox("Do not run from open module"); halt}


[3] Before using perms that presume the "current" module (such as <AttrDef ad = create(...)>) you should explicitely set the "current" module to be the exact one intended for the perm.
The following insures the attribute is created in the home module:

 

 

  • current = mHome
  • create(object type "String" attribute "NewStringAttr")


[3a] Library functions that modify the "current" module should generally reinstate the current module before returning; since the calling program may indeed be (incorrectly) presuming it.

 

 

bool CreateNewAttr(Module mod, string NameAttr, NameType)
{  if (null mod or null NameAttr or null NameType) return(false)
   Module mCurr = current()
   current = mod
   noError()
   AttrDef ad = create(object type NameType attribute NameAttr) 
   if (!null mCurr) current = mCurr   // You cannot "erase" current by setting it to null
   return(null lastError() and !null ad)
}  // end CreateNewAttr()


[4] Functions that deal with Modules should almost always have that Module handle as an input parameter; as in example above. Attempting to "get" the handle is usually clumsy and sometimes erroneous.

-Louie

 


llandale - Sat Oct 27 12:27:35 EDT 2012

Re: "Current" Module advise
PDU - Mon Oct 29 02:04:59 EDT 2012

very useful,

thanks

Re: "Current" Module advise
a.mansoor - Thu Nov 29 19:09:18 EST 2012

This is really useful. Thanks Louie.